﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using TX.Core.Security;
using TX.Data.Ef;
using TX.Passport.Models;

namespace TX.Passport.Persist.Mapping {
    public partial class PassportMapping : IEntityMapping {
    
        public void RegisterTo(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            RegisterAdministrator(modelBuilder);
            RegisterDepartment(modelBuilder);
            RegisterPosition(modelBuilder);
            RegisterPrincipal(modelBuilder);
            RegisterAuthorization(modelBuilder);
            RegisterOperation(modelBuilder);
            RegisterOperationGroup(modelBuilder);
        }
        
        #region AdministratorMapping
        private void RegisterAdministrator(DbModelBuilder modelBuilder)
        {
            //Principal
            var config = modelBuilder.Entity<Administrator>();
            config.ToTable("Administrator");
            config.HasKey(t => t.Id);
            config.Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            
            //
            config.Property(t => t.Id)
                .HasColumnName("Id")
                .IsRequired();
            //
            config.Property(t => t.UserName)
                .HasColumnName("UserName")
                .HasMaxLength(50)
                .IsRequired();
            //
            config.Property(t => t.Password)
                .HasColumnName("Password")
                .HasMaxLength(50)
                .IsRequired();
            //
            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();
            //
            config.Property(t => t.ThisLoginDateTime)
                .HasColumnName("ThisLoginDateTime")
                .IsOptional();
            //
            config.Property(t => t.LastLoginDateTime)
                .HasColumnName("LastLoginDateTime")
                .IsOptional();
            //
            config.Property(t => t.ThisLoginIp)
                .HasColumnName("ThisLoginIp")
                .HasMaxLength(16)
                .IsOptional();
            //
            config.Property(t => t.LastLoginIp)
                .HasColumnName("LastLoginIp")
                .HasMaxLength(16)
                .IsOptional();
            //
            config.Property(t => t.LoginTimes)
                .HasColumnName("LoginTimes")
                .IsRequired();
            //
            config.Property(t => t.PositionId)
                .HasColumnName("PositionId")
                .IsOptional();
            //
            config.Property(t => t.DepartmentId)
                .HasColumnName("DepartmentId")
                .IsOptional();
            //
            config.Property(t => t.State)
                .HasColumnName("State")
                .IsRequired();
            HasOptional(t => t.Department)
                .WithMany(t => t.Administrators)
                .HasForeignKey(d => d.DepartmentId);
            HasOptional(t => t.Position)
                .WithMany(t => t.Administrators)
                .HasForeignKey(d => d.PositionId);
            HasRequired(t => t.Principal)
                .WithOptional(t => t.Administrator);
        }
        #endregion
        #region DepartmentMapping
        private void RegisterDepartment(DbModelBuilder modelBuilder)
        {
            //Principal
            var config = modelBuilder.Entity<Department>();
            config.ToTable("Department");
            config.HasKey(t => t.Id);
            config.Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            
            //
            config.Property(t => t.Id)
                .HasColumnName("Id")
                .IsRequired();
            //
            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();
            //
            config.Property(t => t.Code)
                .HasColumnName("Code")
                .IsOptional();
            HasRequired(t => t.Principal)
                .WithOptional(t => t.Department);
        }
        #endregion
        #region PositionMapping
        private void RegisterPosition(DbModelBuilder modelBuilder)
        {
            //Principal
            var config = modelBuilder.Entity<Position>();
            config.ToTable("Position");
            config.HasKey(t => t.Id);
            config.Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            
            //
            config.Property(t => t.Id)
                .HasColumnName("Id")
                .IsRequired();
            //
            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();
            //
            config.Property(t => t.DepartmentId)
                .HasColumnName("DepartmentId")
                .IsOptional();
            HasOptional(t => t.Department)
                .WithMany(t => t.Positions)
                .HasForeignKey(d => d.DepartmentId);
            HasRequired(t => t.Principal)
                .WithOptional(t => t.Position);
        }
        #endregion
        #region PrincipalMapping
        private void RegisterPrincipal(DbModelBuilder modelBuilder)
        {
            //Principal
            var config = modelBuilder.Entity<Principal>();
            config.ToTable("Principals");
            config.HasKey(t => t.Id);
            config.Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            
            //
            config.Property(t => t.Id)
                .HasColumnName("Id")
                .IsRequired();
        }
        #endregion
        #region AuthorizationMapping
        private void RegisterAuthorization(DbModelBuilder modelBuilder)
        {
            //Principal
            var config = modelBuilder.Entity<Authorization>();
            config.ToTable("Authorization");
            config.HasKey(t => t.OperationId);
            config.Property(t => t.OperationId).HasColumnName("OperationId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            
            //
            config.Property(t => t.OperationId)
                .HasColumnName("OperationId")
                .IsRequired();
            //
            config.Property(t => t.PrincipalId)
                .HasColumnName("PrincipalId")
                .IsRequired();
            //
            config.Property(t => t.State)
                .HasColumnName("State")
                .IsRequired();
            HasRequired(t => t.Operation)
                .WithMany(t => t.Authorizations)
                .HasForeignKey(d => d.OperationId);
        }
        #endregion
        #region OperationMapping
        private void RegisterOperation(DbModelBuilder modelBuilder)
        {
            //Principal
            var config = modelBuilder.Entity<Operation>();
            config.ToTable("Operation");
            config.HasKey(t => t.Id);
            config.Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            
            //
            config.Property(t => t.Code)
                .HasColumnName("Code")
                .IsRequired();
            //
            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();
            //
            config.Property(t => t.Description)
                .HasColumnName("Description")
                .HasMaxLength(255)
                .IsOptional();
            //
            config.Property(t => t.GroupId)
                .HasColumnName("GroupId")
                .IsRequired();
            HasRequired(t => t.GroupOperationGroup)
                .WithMany(t => t.GroupOperations)
                .HasForeignKey(d => d.GroupId);
        }
        #endregion
        #region OperationGroupMapping
        private void RegisterOperationGroup(DbModelBuilder modelBuilder)
        {
            //Principal
            var config = modelBuilder.Entity<OperationGroup>();
            config.ToTable("OperationGroup");
            config.HasKey(t => t.Id);
            config.Property(t => t.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            
            //
            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();
        }
        #endregion
    }
}

